package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.DialogButtonType;
import com.aplink.generic.constant.type.DialogStatus;
import com.aplink.generic.dialog.ApLinkAlertDialog;
import com.aplink.generic.dialog.ApLinkProgressDialog;
import com.aplink.generic.dialog.DialogButton;
import com.aplink.generic.dialog.DialogInfo;
import com.aplink.generic.dialog.SparseDialog;
import com.aplink.generic.util.ResourcesHelper;

public class DialogParser {
	private static final String ACTION = "action";
	private static final String ALERT_DIALOG = "alertDialog";
	private static final String BROADCAST_STATUS = "broadcastStatus";
	private static final String BUTTON_NEGATIVE = "button_negative";
	private static final String BUTTON_NEUTRAL = "button_neutral";
	private static final String BUTTON_POSITIVE = "button_positive";
	private static final String CANCELABLE = "Cancelable";
	private static final String CANCELABLE_ON_TOUCH_OUTSIDE = "CanceledOnTouchOutside";
	private static final String COMPARE_COLUMN = "compareColumn";
	private static final String DIALOG_INFO = "dialogInfo";
	private static final String INPUT_TEXT = "inputText";
	private static final String IS_FINISH_ACTIVITY = "isFinishActivity";
	private static final String IS_FORMAT_HTML = "isFormatHTML";
	private static final String IS_LINK_TO_NEXT_PAGE = "isLinkToNextPage";
	private static final String MESSAGE = "message";
	private static final String PROGRESS_BAR = "progressDialog";
	private static final String STATUS = "status";
	private static final String TITLE = "title";

	private static String getViewTitile(final String viewTitle) {
		return ResourcesHelper.getStringByName(viewTitle);
	}

	public static SparseDialog parseApLinkAlertDialog(Node node) {
		final SparseDialog alertDialogs = new SparseDialog();
		Element element = (Element) node;
		final NodeList nodes = element
		        .getElementsByTagName(DialogParser.ALERT_DIALOG);
		if (nodes.getLength() == 0) {
			return alertDialogs;
		}
		for (int item = 0; item < nodes.getLength(); item++) {
			final ApLinkAlertDialog apLinkAlertDialog = new ApLinkAlertDialog();
			node = nodes.item(item);
			int actionCode = -1;
			if (node.hasAttributes()) {
				String value = XmlPaserUtil.getAttrValue(node,
				        DialogParser.CANCELABLE);
				if (!TextUtils.isEmpty(value) && value.equalsIgnoreCase("Y")) {
					apLinkAlertDialog.setCancelable(true);
				}

				value = XmlPaserUtil.getAttrValue(node,
				        DialogParser.CANCELABLE_ON_TOUCH_OUTSIDE);
				if (!TextUtils.isEmpty(value) && value.equalsIgnoreCase("Y")) {
					apLinkAlertDialog.setCanceledOnTouchOutside(true);
				}

				value = XmlPaserUtil.getAttrValue(node, DialogParser.ACTION);
				if (!TextUtils.isEmpty(value)) {
					actionCode = ActionType.get(value).getCode();
				}
			}
			element = (Element) node;
			final NodeList dialogInfoList = element
			        .getElementsByTagName(DialogParser.DIALOG_INFO);
			for (int i = 0; i < dialogInfoList.getLength(); i++) {
				final Node nodeDialog = dialogInfoList.item(i);
				String dialogKey = null;
				if (nodeDialog.hasAttributes()) {
					dialogKey = XmlPaserUtil.getAttrValue(nodeDialog,
					        DialogParser.STATUS);
				}
				if (nodeDialog.getNodeType() == Node.ELEMENT_NODE) {
					final DialogInfo dialogInfo = DialogParser
					        .parseDialogInfo(nodeDialog);
					apLinkAlertDialog.put(dialogKey, dialogInfo);
				}
			}

			if (!apLinkAlertDialog.contain(DialogStatus.NO_INTERNET_ACCESS
			        .getValue())) {
				DialogInfo noInternetDialog = new DialogInfo();
				noInternetDialog.setText("No Internet access");
				DialogButton dialogButton = new DialogButton();
				dialogButton.setText("OK");
				noInternetDialog.setPositiveButton(dialogButton);
				apLinkAlertDialog.put(
				        DialogStatus.NO_INTERNET_ACCESS.getValue(),
				        noInternetDialog);
			}
			if (!apLinkAlertDialog.contain(DialogStatus.INVALID_TOKEN
			        .getValue())) {
				DialogInfo invalidToken = new DialogInfo();
				DialogButton dialogButton = new DialogButton();
				dialogButton.setText("OK");
				invalidToken.setTitle("Invalid Token");
				invalidToken.setPositiveButton(dialogButton);
				apLinkAlertDialog.put(DialogStatus.INVALID_TOKEN.getValue(),
				        invalidToken);
				alertDialogs.put(actionCode, apLinkAlertDialog);
			}
		}
		return alertDialogs;
	}

	public static ApLinkProgressDialog parseApLinkProgressDialog(Node node) {
		final ApLinkProgressDialog apLinkProgressDialog = new ApLinkProgressDialog();
		Element element = (Element) node;
		final NodeList nodes = element
		        .getElementsByTagName(DialogParser.PROGRESS_BAR);
		if (nodes.getLength() == 0) {
			return null;
		}

		node = nodes.item(0);

		if (node.hasAttributes()) {
			final String canceble = XmlPaserUtil.getAttrValue(node,
			        DialogParser.CANCELABLE);
			if ((canceble != null) && canceble.equalsIgnoreCase("Y")) {
				apLinkProgressDialog.setCancelable(true);
			}

			final String canceledOnTouchOutside = XmlPaserUtil.getAttrValue(
			        node, DialogParser.CANCELABLE_ON_TOUCH_OUTSIDE);
			if ((canceledOnTouchOutside != null)
			        && canceledOnTouchOutside.equalsIgnoreCase("Y")) {
				apLinkProgressDialog.setCanceledOnTouchOutside(true);
			}
		}
		element = (Element) node;
		final NodeList dialogInfoList = element
		        .getElementsByTagName(DialogParser.DIALOG_INFO);

		for (int i = 0; i < dialogInfoList.getLength(); i++) {

			final Node nodeDialog = dialogInfoList.item(i);
			String dialogKey = null;
			if (nodeDialog.hasAttributes()) {
				dialogKey = XmlPaserUtil.getAttrValue(nodeDialog,
				        DialogParser.STATUS);
			}
			if (nodeDialog.getNodeType() == Node.ELEMENT_NODE) {
				final DialogInfo dialogInfo = DialogParser
				        .parseDialogInfo(nodeDialog);
				apLinkProgressDialog.put(dialogKey, dialogInfo);
			}
		}

		return apLinkProgressDialog;
	}

	private static DialogButton parseDialogButton(final Node nodeDialogButton) {
		final DialogButton dialogButton = new DialogButton();
		if (nodeDialogButton.hasAttributes()) {
			final String isFinishActivity = XmlPaserUtil.getAttrValue(
			        nodeDialogButton, DialogParser.IS_FINISH_ACTIVITY);
			if ((isFinishActivity != null)
			        && isFinishActivity.equalsIgnoreCase("Y")) {
				dialogButton.setFinishActivity(true);
			}
			final String isLinkToNextPage = XmlPaserUtil.getAttrValue(
			        nodeDialogButton, DialogParser.IS_LINK_TO_NEXT_PAGE);
			if ((isLinkToNextPage != null)
			        && isLinkToNextPage.equalsIgnoreCase("Y")) {
				dialogButton.setLinkToNextPage(true);
			}

			final String broadCastStatus = XmlPaserUtil.getAttrValue(
			        nodeDialogButton, DialogParser.BROADCAST_STATUS);
			if (!TextUtils.isEmpty(broadCastStatus)) {
				final ActionType actionType = ActionType.get(broadCastStatus);
				dialogButton.setActionType(actionType);
			}
		}
		final String value = DialogParser.getViewTitile(XmlPaserUtil
		        .getNodeValue(nodeDialogButton));
		dialogButton.setText(value);
		return dialogButton;
	}

	public static DialogInfo parseDialogInfo(Node nodeDialog) {
		final DialogInfo dialogInfo = new DialogInfo();
		if (nodeDialog.hasAttributes()) {
			final String isFormatHTML = XmlPaserUtil.getAttrValue(nodeDialog,
			        DialogParser.IS_FORMAT_HTML);
			if (!TextUtils.isEmpty(isFormatHTML)) {
				if (isFormatHTML.equalsIgnoreCase("Y")) {
					dialogInfo.setFormatHTML(true);
				}
			}
		}
		if (nodeDialog.getNodeType() == Node.ELEMENT_NODE) {
			final Element element = (Element) nodeDialog;
			// Get progress bar title
			NodeList nodes = element.getElementsByTagName(DialogParser.TITLE);
			if (nodes.getLength() != 0) {
				nodeDialog = nodes.item(0);
				final String value = DialogParser.getViewTitile(XmlPaserUtil
				        .getNodeValue(nodeDialog));
				dialogInfo.setTitle(value);
			}

			// Get progress bar message
			nodes = element.getElementsByTagName(DialogParser.MESSAGE);
			if (nodes.getLength() != 0) {
				nodeDialog = nodes.item(0);
				final String value = DialogParser.getViewTitile(XmlPaserUtil
				        .getNodeValue(nodeDialog));
				dialogInfo.setText(value);
			}
			// Get progress bar message
			nodes = element.getElementsByTagName(DialogParser.INPUT_TEXT);
			if (nodes.getLength() != 0) {
				nodeDialog = nodes.item(0);
				dialogInfo.setCompareColumn(XmlPaserUtil.getAttrValue(
				        nodeDialog, COMPARE_COLUMN));
				final String value = DialogParser.getViewTitile(XmlPaserUtil
				        .getNodeValue(nodeDialog));
				dialogInfo.setInputText(value);
			}
			// Get progress bar BUTTON_POSITIVE
			nodes = element.getElementsByTagName(DialogParser.BUTTON_POSITIVE);
			if (nodes.getLength() != 0) {
				nodeDialog = nodes.item(0);
				final DialogButton dialogButton = DialogParser
				        .parseDialogButton(nodeDialog);
				dialogButton.setButtonType(DialogButtonType.Positive);
				dialogInfo.setPositiveButton(dialogButton);
			}

			// Get progress bar BUTTON_NEGATIVE
			nodes = element.getElementsByTagName(DialogParser.BUTTON_NEGATIVE);
			if (nodes.getLength() != 0) {
				nodeDialog = nodes.item(0);
				final DialogButton dialogButton = DialogParser
				        .parseDialogButton(nodeDialog);
				dialogButton.setButtonType(DialogButtonType.Negative);
				dialogInfo.setNegativeButton(dialogButton);
			}

			// Get progress bar BUTTON_NEUTRAL
			nodes = element.getElementsByTagName(DialogParser.BUTTON_NEUTRAL);
			if (nodes.getLength() != 0) {
				nodeDialog = nodes.item(0);
				final DialogButton dialogButton = DialogParser
				        .parseDialogButton(nodeDialog);
				dialogButton.setButtonType(DialogButtonType.Neutral);
				dialogInfo.setNeutralButton(dialogButton);
			}
		}
		return dialogInfo;
	}
}
